package com.yc.resfoods.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.bean.Resuser;
import com.yc.resfoods.dao.ResuserDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @ClassName JwtUserDetailsService
 * @since: 2023/8/21 11:44
 * @auth: kirito
 * @description:
 **/
@Service
@Slf4j
public class JwtUserDetailsService implements UserDetailsService {
    @Autowired
    private ResuserDao resuserDao;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Resuser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Resuser resuser = resuserDao.selectOne(wrapper);
        if (resuser != null) {
            return new User(resuser.getUsername(), resuser.getPwd(), new ArrayList<>());
        }else {
            throw new UsernameNotFoundException("user not found with username: " + username);
        }
    }
}
